/*
' Copyright (c) 2012 DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Web;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.UI.UserControls;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.mytargetmodule.Components;


namespace DotNetNuke.Modules.mytargetmodule
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Editmytargetmodule class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from mytargetmoduleModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Edit : mytargetmoduleModuleBase
    {
        DateTime outputdate;
        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                //Implement your edit logic for your module
                if(!Page.IsPostBack)
                {
                    ddlAssignedUser.DataSource = UserController.GetUsers(PortalId);
                    ddlAssignedUser.DataTextField = "UserName";
                    ddlAssignedUser.DataValueField ="UserID"; 
                    ddlAssignedUser.DataBind();
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        protected void lkbcancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
            
          
        }

        protected void lkbsubmit_Click(object sender, EventArgs e)
        {
            Task t = new Task();
            
            t.AssignedUserID = Convert.ToInt16(ddlAssignedUser.SelectedValue); 
            //t.CompletedCndate =Convert.ToDateTime(txtcompletiondate.Text.Trim());
            t.CreatedByUserID = UserId;
            t.CreatedDate = DateTime.Now;
           // t.TargetCompletionDAte = Convert.ToDateTime(txttargetcompletiondate.Text.Trim());
            t.Taskname = txtname.Text.Trim();
            t.TaskDescription = txtdescription.Text.Trim();

            TaskController.savetask(t, TabId);
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());

            if(DateTime.TryParse(txtcompletiondate.Text.Trim(), out outputdate))
            {
                t.CompletedCndate = outputdate;
            }

            if(DateTime.TryParse(txttargetcompletiondate.Text.Trim(),out outputdate))
            {
                t.TargetCompletionDAte = outputdate;
            }
        }

    }

       

}